<template>
  <el-drawer
    :title="title"
    :visible.sync="dialogVisible"
    direction="rtl"
    size="50%"
    :show-close="false"
  >
    <div class="drawerd">
      <div class="btn-sticky">
        <el-button
          type="primary"
          size="mini"
          class="u-m-l-10"
          v-throttle="() => doAction('submitForm')"
          >保存</el-button
        >
        <el-button
          type="primary"
          size="mini"
          class="btn-stop-use"
          v-throttle="() => $emit('cancel')"
          >返回</el-button
        >
      </div>
      <div>
        <div class="search-bar d-flex u-col-center u-m-b-20 u-flex-wrap">
          <el-input
            style="width: auto"
            size="mini"
            class="u-m-l-10"
            v-model="queryParams.changpinmiaoshu"
            placeholder="地点名称"
          ></el-input>
          <el-input
            style="width: auto"
            size="mini"
            class="u-m-l-10"
            v-model="queryParams.lianheguonbianma"
            placeholder="详细地址"
          ></el-input>

          <el-button
            size="mini"
            class="u-m-l-10"
            type="primary"
            v-throttle="() => doAction('search')"
            >查询</el-button
          >
        </div>
      </div>
      <!-- NOTE: 用于设置表格最大高度 -->
      <div class="u-flex-1" ref="table">
        <y-table
          ref="tableInner"
          :row-key="getRowKey"
          :data="tableData"
          :height="tableHeight"
          :columns="tableColumn"
          :defaultColumnConfig="defaultColumnConfig"
          @selection-change="handleSelectionChange"
          @row-click="handleRowClick"
        >
        </y-table>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import { debounce } from "throttle-debounce";
import TableRowClickMixin from "@/mixins/table/RowClickMixin";
// 处理table操作_查询
const TableMixin = {
  data() {
    return {
      defaultColumnConfig: {
        align: "center",
      },

      tableColumn: [
        {
          type: "selection",
          width: 55,
        },
        {
          prop: "weixinpinfenlei",
          label: "地址代码",
        },
        {
          prop: "chanpingmiaoshu",
          label: "地点名称",
        },
        {
          prop: "lianheguobianma",
          label: "详细地址",
          width: "300",
          showOverflowTooltip: true,
        },

        {
          prop: "hupwubaozhuanle",
          label: "所属公司",
        },
      ],

      tableData: [
        {
          id: 10191,
          chanpingmiaoshu: "黄埔码头",
          lianheguobianma: "广州黄埔港",
          weixinpinfenlei: "111292",
          hupwubaozhuanle: "品易智联",
        },
        {
          id: 10192,
          chanpingmiaoshu: "长沙港",
          lianheguobianma: "湖南省长沙市湘江中路",
          weixinpinfenlei: "11123",
          hupwubaozhuanle: "品易智联",
        },
        {
          id: 10193,
          chanpingmiaoshu: "科迪化工",
          lianheguobianma: "安徽省淮南市高新技术开发区",
          weixinpinfenlei: "111295",
          hupwubaozhuanle: "品易智联",
        },
      ],
      // 选择的项
      multipleSelection: [],
      tableHeight: 0, // 表格高度
    };
  },

  created() {
    this.debounceHandleResize = debounce(
      250,
      this.calcTableAviableHeight.bind(this)
    );
  },

  methods: {
    getRowKey(row) {
      return row.ID;
    },

    //查询操作（条件总数据）-安全检查
    handleSubmit(order) {
      if (this.queryRenYuanMingCheng !== "") {
        this.queryParams.xingming = this.queryRenYuanMingCheng;
      }
      let q = deepClone(this.queryParams);
      console.log(q);

      let queryItemAdapter = (key, val) => `${key}>='${val}'`;
      let queryItemAdapter2 = (key, val) => `${key}<='${val}'`;

      let computeWhereStr = (obj) => {
        let renyuanID =
          this.renyuanId !== "" ? ` and a.ID not in ('${this.renyuanId}')` : "";
        let whereStr = `1=1 and a.IsDelete=${q.shifou}${renyuanID}`;
        let keys = Object.keys(obj);
        if (keys.length === 0) return whereStr;
        let ret = [];

        keys.forEach((key) => {
          let val = obj[key];
          if (val === undefined) return;
          if (val === null) return;
          if (val === "") return;

          // 特殊逻辑处理
          if (key === "youxiaoqi") {
            if (val.length > 0) {
              ret.push(queryItemAdapter("a.youxiaoqi", val[0]));
              ret.push(queryItemAdapter2("a.youxiaoqi", val[1]));
            }
            return;
          }
          if (key !== "shifou") {
            if (key === "zhengzhaohaoma") {
              ret.push(`a.${key} like '%${val}%'`);
            } else {
              ret.push(`a.${key}='${val}'`);
            }
          }
        });
        if (ret.length === 0) return whereStr;
        let istr =
          ret.join(" and ") + ` and a.IsDelete=${q.shifou}${renyuanID}`;
        return istr;
      };

      let orderstr = "CreateDate desc"; // ziduan asc
      if (order !== "" && order !== undefined) {
        orderstr = order;
      }

      let whereStr = computeWhereStr(this.queryParams);

      let params = {
        pageNum: this.pagination.current,
        pageSize: this.pagination.size,
        where: whereStr,
        order: orderstr,
      };
      // 获取查询请求
      this.loadList(params);
    },

    /**
     * 计算表格可用高度
     */
    calcTableAviableHeight() {
      let el = this.$refs.table;
      if (el && el.getBoundingClientRect) {
        let { top } = el.getBoundingClientRect();
        let h = 50 + 15 + 27 + top;
        let tableHeight = window.innerHeight - h - 33;
        // 设置table的高度
        el.style.height = tableHeight + "px";
        this.tableHeight = tableHeight;
      }
    },
  },

  mounted() {
    let $__func = this.debounceHandleResize.bind(this);
    window.addEventListener("resize", $__func);

    this.$nextTick(() => {
      this.calcTableAviableHeight();
    });
  },
};
export default {
  components: {},
  mixins: [TableMixin, TableRowClickMixin],
  props: {
    rowid: {
      type: Array,
    },
  },
  data() {
    return {
      dialogVisible: true,
      title: "关联卸货地",
      queryParams: {
        changpinmiaoshu: "",
        lianheguonbianma: "",
      },
    };
  },
  methods: {
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    doAction(actionType, data) {
      /**
       * Action处理策略
       */
      const actionStrategies = {
        /**
         * 查询
         */
        search: () => {
          //   this.handleFormSubmit()
        },

        // 提交
        submitForm: () => {
          let len = this.multipleSelection.length;
          if (!len) {
            return this.$notify.warning({
              title: "消息",
              message: "请选择一条数据进行操作！",
            });
          }
          //   获取一条数据并返回
          let data = this.multipleSelection;
          console.log("发送请求", this.rowid, data);
          this.$emit("done");
          this.sendOrdersSuccess();
        },
      };

      const actionStrategy = actionStrategies[actionType];
      if (!actionStrategy) return;
      actionStrategy(data);
    },

    // 提交成功
    sendOrdersSuccess() {
      this.$notify.success({
        title: "消息",
        message: "保存成功",
        position: "bottom-right",
      });
      this.$emit("done");
    },
  },
  created() {
    console.log("打印结果传值", this.rowid);
  },
};
</script>

<style lang="scss" scoped>
.drawerd {
  padding: 0 20px;
}
.btn-stop-use {
  color: #444444;
  background: rgba(192, 192, 192, 1);
  border: 1px solid rgba(176, 177, 177, 1);
}
.btn-sticky {
  text-align: right;
  position: sticky;
  top: 0;
  background-color: #fff;
  z-index: 22;
}
</style>
